﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Phoenix.Service;
using Phoenix.Service.Contracts;
using Phoenix.Service.Model;
using Phoenix.Common.Util;
using Phoenix.Enum;
namespace Phoenix.AdminSite.Controllers
{
    public class AdminUserController : ControllerBase
    {
        IAdminUserService IAdminUser = new AdminUserService();
        IOrganizationService IOrganization = new OrganizationService();
        IIDCreaterService IIDCreater = new IDCreaterService();
        IDepartmentService IIDepartment = new DepartmentService();

        public ActionResult Index(int? pageindex)
        {
            string beginDate = Request["txtBeginDate"] ?? "";
            string endDate = Request["txtEndDate"] ?? "";
            string userName = Request["txtUserName"] ?? "";
            string trueName = Request["txtTrueName"] ?? "";

            // 分页参数
            int pageSize = 10;
            int pIndex = pageindex.GetValueOrDefault(1);
            int recordCount = 0;

            IList<AdminUserModel> model = IAdminUser.GetPagedList(pageSize, pIndex, trueName, userName, null, null, out recordCount);

            ViewBag.PagerModel = Service.PagerService.ProcessPagerModel(pageSize, pIndex.ToString(), recordCount, "", pageSize);

            // 保存参数
            //ViewData["txtRoleName"] = roleName;
            ViewBag.BeginDate = beginDate;
            ViewBag.EndDate = endDate;
            ViewBag.UserName = userName;
            ViewBag.TrueName = trueName;

            return View(model);
          
        }

        public ActionResult Edit(int? userID)
        {

            //if (!accountID.HasValue) { return Content(CommHelper.JsAlertAndRedirect("ID错误!", "/account/index")); }

            //AccountModel account = accountService.Get(accountID.Value);
            //GetEditView(accountID.Value); // 部门 & 职位

            //return View(account);

            AdminUserModel model = IAdminUser.GetByUserID(userID.Value);

            ViewBag.DepartmentName = IIDepartment.GetDepartmentName(model.DepartmentID);
            //ViewBag.JobTitle = jobTitlesService.GetJobName(account.JobID); // 保存职位名称


            ViewData["OrganizationList"] = IOrganization.GetPath();

            return View(model);

        }

        public ActionResult Add()
        {
            ViewData["OrganizationList"] = IOrganization.GetPath();
            return View();
        }

        public ActionResult Update(FormCollection collection)
        {

            if (!string.IsNullOrEmpty(collection["hdUserID"]))
            {
                int userID = int.Parse(collection["hdUserID"]);

                AdminUserModel model = IAdminUser.GetByUserID(userID);
                model.TrueName = collection["txtTrueName"];
                model.EMail = collection["txtEMail"];
                //model.OrgID = int.Parse(collection["sltOrg"]);
                model.Memo = collection["txtMemo"];
                model.Telephone = collection["txtTelephone"];
                model.UpdateTime = DateTime.Now;
                IAdminUser.UpdateByUserID(model);
                return ShowMsg("成功", Common.ActionStatus.OK, "");
            }
            else
            {
                return ShowMsg("失败", Common.ActionStatus.Error, "");
            }
           
        }

        public ActionResult Save(FormCollection collection)
        {
            AdminUserModel model = new AdminUserModel();
            model.UserID = IIDCreater.GetNewID("AdminID", "管理员编号", 1000);
            model.UserName = collection["txtUserName"];
            model.TrueName = collection["txtTrueName"];
            model.EMail = collection["txtEMail"];
           
            model.Memo = collection["txtMemo"];
            model.Password = collection["txtPassword"];
            model.Telephone = collection["txtTelephone"];
            model.UpdateTime = DateTime.MinValue;
            model.Visitation = 0;
            model.LastLoginTime = DateTime.MinValue;
            model.CreateTime = DateTime.Now;
            model.DataPermisType = 1;
            model.LockTime = DateTime.MinValue;
            model.Status = 1;
            
            IAdminUser.AddAdminUser(model);

            return ShowMsg("成功", Common.ActionStatus.OK, "");

        }

        //检测用户名是否存在
        public ActionResult CheckUserName()
        {
            string userName = Request.QueryString["txtUserName"];
            bool b = IAdminUser.GetExistsUserName(userName);

            return Content(b == true ? "1" : "0");
        }


    }
}
